#ifndef VIDEODECODETHREAD_H
#define VIDEODECODETHREAD_H

#include <QThread>
#include <QImage>
#include <QMutex>
#include <QString>

extern "C" {
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libswscale/swscale.h>
}

class VideoDecoderThread : public QThread
{
    Q_OBJECT
public:
    explicit VideoDecoderThread(QObject *parent = nullptr);
    ~VideoDecoderThread();

    void setVideoFile(const QString &filePath);
    void stop();
    void pause(bool paused);
    void cleanup();
    bool isRunning = false;


signals:
    void frameReady(const QImage &image);
    void errorOccurred(const QString &error);
    void playbackFinished();

protected:
    void run() override;
private:
    QString videoFilePath;
    AVFormatContext *formatCtx = nullptr;
    AVCodecContext *codecCtx = nullptr;
    SwsContext *swsCtx = nullptr;
    AVFrame *frame = nullptr;
    AVFrame *rgbFrame = nullptr;
    uint8_t *rgbBuffer = nullptr;
    int videoStreamIndex = -1;
    bool isPaused = false;
    QMutex mutex;

    bool initializeDecoder();

    
};

#endif // !